package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.OrderDao;
import com.qf.xiaomi.pojo.OrderDetail;
import com.qf.xiaomi.pojo.OrderList;
import com.qf.xiaomi.pojo.ProduceOrderInfo;
import com.qf.xiaomi.pojo.TbAddress;
import com.qf.xiaomi.utils.DruidDButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Created by 马英超 on 2022/8/20 17:34
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class OrderDaoImpl implements OrderDao {
    /**
     * 根据购物车内商品信息向订单表插入数据
     * @param userId
     * @param orderCode
     * @param sum
     * @param addressId
     * @return
     */

    @Override
    public int insertOrder(int userId, String orderCode, int sum,int addressId) {
        String sql="insert into tb_order(id,uid,money,aid,createtime) values(?,?,?,?,now())";
        QueryRunner queryRunner=new QueryRunner();
         int updateResult=0;
        try {
             updateResult = queryRunner.update(DruidDButils.getDruidConn(), sql, orderCode, userId, sum, addressId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return updateResult ;
    }

    /**
     * 订单下单成功后需要清空购物车内商品信息
     * @param userId
     * @return
     */
    @Override
    public int clearCart(int userId) {
        String sql="delete from tb_cart where id=?";
        QueryRunner queryRunner=new QueryRunner();
         int update=0;
        try {
           update = queryRunner.update(DruidDButils.getDruidConn(), sql, userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }

    //查询订单信息
    @Override
    public List<OrderList> queryOrderInfo(int userId){
        String sql="select  a.id ,a.money ,a.status ,a.createtime,b.detail from tb_order a left join  tb_address b on a.aid=b.id where a.uid=?";
        QueryRunner queryRunner=new QueryRunner();
        try {

            final List<OrderList> orderListList = queryRunner.query(DruidDButils.getDruidConn(), sql, new BeanListHandler<>(OrderList.class), userId);
            if (orderListList.size()>0)
                return orderListList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    //插入订单中间表
    @Override
    public int addOrderDetail(String orderCode, int pid, int num, int price) {
        String sql="INSERT INTO tb_orderdetail(Oid, pid, num, Money) VALUES(?, ?,?, ?)";
        QueryRunner queryRunner=new QueryRunner();
        try {

             final int update = queryRunner.update(DruidDButils.getDruidConn(), sql, orderCode, pid, num, price);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //查询订单详情
    @Override
    public OrderDetail queryOrderDetailInfo(String orderId, int userId) {
        //查询订单信息
        String sql1="select o.id, o.createtime as time ,o.money from tb_order o where o.id=? and o.uid=?";
        //查询地址
        String sql2="select t.* from tb_address t where t.id = (select a.aid from tb_order a where a.id=?  and a.uid=?)";
        //查询产品信息
        String sql3="select p.picture,p.pubdate,p.name,p.star,o.num,o.money,o.num*o.money as sumMoney from  tb_order a left join  tb_orderdetail o on o.oid=a.id left join   tb_goods_kz p on p.id =o.pid where  a.id=?  and a.uid=?";

        QueryRunner queryRunner=new QueryRunner();
        //查询订单信息
        try {
            OrderDetail orderDetail = queryRunner.query(DruidDButils.getDruidConn(), sql1, new BeanHandler<>(OrderDetail.class), orderId,userId);
            TbAddress address = queryRunner.query(DruidDButils.getDruidConn(), sql2, new BeanHandler<>(TbAddress.class), orderId,userId);
            final List<ProduceOrderInfo> orderInfoList = queryRunner.query(DruidDButils.getDruidConn(), sql3, new BeanListHandler<>(ProduceOrderInfo.class), orderId,userId);

             orderDetail.setAddress(address);
             orderDetail.setList(orderInfoList);

            if (orderDetail!=null&&address!=null&&orderInfoList.size()>0)
                return orderDetail;
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
